package cn.scw.project.goods.service.impl;

import cn.scw.common.constant.DictTypeConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.goods.domain.GCoupon;
import cn.scw.project.goods.domain.netework.bean.GCouponAddReq;
import cn.scw.project.goods.domain.netework.bean.GCouponListReq;
import cn.scw.project.goods.domain.vo.GCouponVo;
import cn.scw.project.goods.mapper.GCouponMapper;
import cn.scw.project.goods.service.IGCouponService;
import cn.scw.project.goods.service.IGCouponUserService;
import cn.scw.project.system.domain.SysUser;
import cn.scw.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 优惠券Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GCouponServiceImpl implements IGCouponService {
    @Autowired
    private GCouponMapper gCouponMapper;
    @Autowired
    private IGCouponUserService gCouponUserService;
    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询优惠券
     *
     * @param id 优惠券ID
     * @return 优惠券
     */
    @Override
    public GCoupon selectGCouponById(Long id) {
        return gCouponMapper.selectGCouponById(id);
    }

    @Override
    public GCouponVo selectGCouponVoById(Long id) {
        return gCouponMapper.selectGCouponVoById(id);
    }

    /**
     * 查询优惠券列表
     *
     * @param req 优惠券
     * @return 优惠券
     */
    @Override
    public List<GCouponVo> selectGCouponList(GCouponListReq req) {
        return gCouponMapper.selectGCouponList(req);
    }

    /**
     * 新增优惠券
     *
     * @param req 优惠券
     * @return 结果
     */
    @Override
    public int insertGCoupon(GCouponAddReq req) {
        GCoupon gCoupon = new GCoupon();
        ToolUtils.copyPropertiesIgnoreNull(req, gCoupon);
        if (req.getCount() != null && req.getCount() <= 0)
            throw new CustomException("请输入优惠券数量");
        if (req.getMoney() != null && req.getMoney() <= 0)
            throw new CustomException("请输入优惠券金额");
        if (req.getStartDate()==null)
            throw new CustomException("请选择使用开始时间");
        if (req.getEndDate()==null)
            throw new CustomException("请选择使用结束时间");
        if (req.getStartDate().after(req.getEndDate())) throw new CustomException("请选择正确的使用期限");
        if (!iSysDictDataService.existsDictLabel(DictTypeConsts.COUPON_USE_SCOPE, req.getUseScopeCode()))
            throw new CustomException("请选择正确的使用范围");
        return gCouponMapper.insertGCoupon(gCoupon);
    }

    /**
     * 修改优惠券
     *
     * @param req 优惠券
     * @return 结果
     */
    @Override
    public int updateGCoupon(GCouponAddReq req) {
        if (req.getCount() != null && req.getCount() <= 0) throw new CustomException("请输入优惠券数量");
        if (req.getMoney() != null && req.getMoney() <= 0) throw new CustomException("请输入优惠券金额");

        GCoupon gCoupon = this.selectGCouponById(req.getId());
        if (gCoupon == null) throw new CustomException("优惠券不存在");
        ToolUtils.copyPropertiesIgnoreNull(req, gCoupon);

        if (gCoupon.getStartDate().after(gCoupon.getEndDate())) throw new CustomException("请选择正确的使用期限");
        if (!iSysDictDataService.existsDictLabel(DictTypeConsts.COUPON_USE_SCOPE, gCoupon.getUseScopeCode()))
            throw new CustomException("请选择正确的使用范围");
        return gCouponMapper.updateGCoupon(gCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券ID
     * @return 结果
     */
    @Override
    public int deleteGCouponByIds(Long[] ids) {
        return gCouponMapper.deleteGCouponByIds(ids);
    }

    /**
     * 删除优惠券信息
     *
     * @param id 优惠券ID
     * @return 结果
     */
    @Override
    public int deleteGCouponById(Long id) {
        return gCouponMapper.deleteGCouponById(id);
    }

    @Override
    public List<GCouponVo> selectUsableList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return gCouponMapper.selectUsableList(user);
    }


    public int updateGCouponResidue(Long[] ids) {
        return gCouponMapper.updateGCouponResidue(ids);
    }

    /**
     * 自动更新是否过期
     */
    @Transactional
    @Override
    public void autoUpdIsOverdue() {
        this.gCouponMapper.updateOverdue(new Date());
        gCouponUserService.updateStatus();
    }
}
